var $rv = $rv || {}; //Rise Vision namespace

console.log(launchData);
$rv.debug = launchData.debugMode;

onload = function () {
    console.log('body.onload is called');

    $rv.browser = document.querySelector('#viewer');
    if ($rv.debug) {
    	onresize = resizeBrowser;

//      document.querySelector('#btClose').onclick = function() {
//    	window.close();
//    };

    	var btRefresh = document.querySelector('#btRefresh')
        if (btRefresh) {
        	btRefresh.style.display = "inline";
        	btRefresh.onclick = function() {
                if ($rv.browser) {
                	$rv.browser.reload();
                }
        	};
        }

    	var btCacheStatus = document.querySelector('#btCacheStatus')
        if (btCacheStatus) {
        	btCacheStatus.style.display = "inline";
        	btCacheStatus.onclick = function() {
            	$rv.cache.printStats();
            	
        	};
        }

    }

    $rv.config = new rvConfig();
    $rv.config.init(onConfigLoad);

};

var onConfigLoad = function(erroCode) {
    $rv.cache = new rvCache();
    $rv.cache.init($rv.config);
    
    $rv.player = new rvPlayer();
    $rv.player.init($rv.config);

//    if ($rv.config.screenWidth && $rv.config.screenHeight) {
//    	console.log("screen size is loaded from cache");
//    	window.resizeTo($rv.config.screenWidth, $rv.config.screenHeight);
//    } else {
//    	console.log("screen size is NOT loaded from cache");
//    }
    
    resizeBrowser();
	
};

function resizeBrowser() {
	$rv.heightOffset = $rv.debug ? 50 : 0;
    if ($rv.browser) {
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight - $rv.heightOffset;
        $rv.browser.style.width = windowWidth + "px";
        $rv.browser.style.height = windowHeight + "px";
        //$rv.config.saveScreenSize(windowWidth, windowHeight);
    }
} 

$rv.close = function() {
	window.close();
};

